package com.qidian.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.buy.mapper.ItemDescMapper;
import com.qidian.buy.mapper.ItemMapper;
import com.qidian.buy.pojo.Item;
import com.qidian.buy.pojo.ItemDesc;
import com.qidian.buy.service.ItemService;
import com.qidian.buy.util.pojo.QdResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2023/9/9 16:19
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.DEFAULT)
    @Override
    public QdResult save(Item item, String itemDesc, String speIds) {
        // 保存商品信息，获取商品的编号
        // MyBatisPlus在的insert方法中，默认会返回自增列的值到对象中。
        int itemInsertResult = itemMapper.insert(item);
        if(itemInsertResult == 1){// 商品添加成功
            ItemDesc desc = new ItemDesc();
            desc.setItemDesc(itemDesc);
            // 给商品详情添加id
            desc.setItemId(item.getItemId());
            // 保存商品详情
            itemDescMapper.insert(desc);
            // 保存商品的规格关系
            String[] ids = speIds.split("_");
            // 批量保存商品与规格之间的关系
            itemMapper.insertItemAndSpeRelation(item.getItemId(),ids);
            // 发送消息
        }

        return QdResult.ok();
    }

    public QdResult saveWrapper(Item item, String itemDesc, String speIds){
        // 这里必须通过AopContext.currentProxy();获取当前service的代理对象。
        ItemService itemService = (ItemService) AopContext.currentProxy();
        QdResult result = itemService.save(item,itemDesc,speIds);
        // 发送消息
        if(result.getCode() == 200){
            // 这里的所有的key和name都是可以进行配置的，我这里暂时写死    这里发送的内容一定要转换为字符串，不要发送Long对象
            rabbitTemplate.convertAndSend("item-add-exchange","itemAdd",item.getItemId().toString());
        }
        return result;
    }

    @Override
    public QdResult queryByPage(Item item, int pageNum, int limit) {
        IPage<Item> page = new Page<>(pageNum,limit);
//        QueryWrapper<Item> wrapper = new QueryWrapper<>();
//        if(item != null) {
//            wrapper.like(item.getTitle() != null && !item.getTitle().equals(""),"title",item.getTitle());
//            wrapper.eq(item.getStatus() != null && item.getStatus() != 0,"status",item.getStatus());
//            wrapper.eq(item.getCatId() != null && item.getCatId() != 0,"cat_id",item.getCatId());
//            wrapper.ge(item.getCreateTimeStart() != null ,"create_time",item.getCreateTimeStart());
//            wrapper.le(item.getCreateTimeEnd() != null,"create_time",item.getCreateTimeEnd());
//            wrapper.ge(item.getModifyTimeStart() != null,"modify_time",item.getModifyTimeStart());
//            wrapper.le(item.getModifyTimeEnd() != null,"modify_time",item.getModifyTimeEnd());
//            wrapper.le(item.getPriceStart() != null,"price",item.getPriceStart());
//            wrapper.ge(item.getPriceEnd() != null,"price",item.getPriceEnd());
//        }
        return QdResult.ok(itemMapper.selectByPage(page,item));
    }

    @Override
    public QdResult queryById(Long itemId) {
        return QdResult.ok(itemMapper.selectByItemId(itemId));
    }
}
